package com.chainyoung.component_tmperson.ui.mvp.presenter;

import com.chainyoung.common.appcommon.entity.CommonList;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.entity.Reward;
import com.chainyoung.component_tmperson.ui.mvp.contract.RewardContract;


import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;


public class RewardPresenter extends BasePresenter<RewardContract.Model, RewardContract.View> {

    @Inject
    public RewardPresenter(RewardContract.Model model, RewardContract.View rootView) {
        super(model, rootView);
    }


    public void getRewardList(TreeMap<String, Object> map) {

        addSubscribe(mModel.getRewardList(map)
                .compose(RxUtils.<CommonList<Reward>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<CommonList<Reward>>(mView, false) {
                    @Override
                    public void onNext(CommonList<Reward> data) {
                        mView.onGetRewardList(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.onGetRewardList(null);
                                return;
                            }
                        }
                        mView.onGetRewardListFailed();
                        super.onError(e);
                    }
                }));
    }


}





